package work.stackandqueque;

import java.util.Stack;

public class Problem20 {
    public boolean isValid(String s) {

        Stack<Character> stack = new Stack();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') stack.push(s.charAt(i));
            else if (s.charAt(i) == ')' && !stack.empty()) {
                Character pop = stack.pop();
                if (pop!= '(') return false;
            }
            else if (s.charAt(i) == '}'&& !stack.empty()) {
                Character pop = stack.pop();
                if (pop!= '{') return false;
            }
            else if (s.charAt(i) == ']'&& !stack.empty()) {
                Character pop = stack.pop();
                if (pop!= '[') return false;
            }
            else return false;
        }
        if (stack.empty()) return true;
        else return false;

    }
}
